package com.atguigu.crowd.handler;


import com.atguigu.crowd.entity.po.MemberPO;
import com.atguigu.crowd.service.MemberService;
import com.atguigu.crowd.util.CrowdConstant;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class MemberProviderHandler {

    @Autowired
    private MemberService memberService;


    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPO memberPO){
        try {
            memberService.saveMember(memberPO);

            return ResultEntity.successData();
        }catch (Exception e) {
            //账号被占用异常
            if(e instanceof DuplicateKeyException){
                return ResultEntity.errorData(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
            }

            return ResultEntity.errorData(e.getMessage());
        }

    }

    @RequestMapping(value = "/get/memberpo/by/loginacct/remote")
    public ResultEntity<MemberPO> getMemberPOByLoginAcctRemote(@RequestParam("loginacct") String loginacct){

        try {
            MemberPO memberPO = memberService.getMemberPOByLoginAcct(loginacct);


            return ResultEntity.successResuleData(memberPO);

        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.errorData(e.getMessage());
        }
        


    }


}
